#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;
const int N = 200010, M = 1000010;
int T, n;
LL q[N];
LL s[N], f[N][2];
LL t[M][2];
bool st[M];

int lowbit(int x)
{
    return x & -x;
}
void modify(int k, int id, LL x)
{
    for (int i = k; i < M; i += lowbit(i)) t[i][id] = max(t[i][id], x);
}
LL query(int k, int id)
{
    LL res = 0;
    for (int i = k; i > 0; i -= lowbit(i)) res = max(res, t[i][id]);
    return res;
}

LL g(int id)
{
    if (q[id] == q[id + 1]) return q[id];
    else return 0;
}

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);

    scanf("%d", &T);

    while (T --)
    {
        s[0] = s[1] = 0;
        memset(f, 0, sizeof f);
        memset(t, 0, sizeof t);
        memset(st, false, sizeof st);

        scanf("%d", &n);
        for (int i = 1; i <= n; i ++) scanf("%lld", &q[i]);

        for (int i = 2; i <= n; i ++)
            if (q[i] == q[i - 1])
                s[i] = s[i - 1] + q[i];

        for (int i = 1; i <= n; i ++)
        {
            if (!st[(int)q[i]]) f[i][0] = f[i][1] = max(f[i - 1][0], f[i - 1][1]);
            else
            {
                f[i][0] = query((int)q[i], 0) + q[i] + s[i - 1];
                f[i][1] = query((int)q[i], 1) + q[i] + s[i - 1];
            }
            modify((int)q[i], 0, f[i][0] - s[i] - g(i));
            modify((int)q[i], 1, f[i][1] - s[i] - g(i));
            st[(int)q[i]] = true;
        }
        printf("%lld\n", max(f[n][0], f[n][1]));
    }

    return 0;
}
